<?php

namespace app\admin\model\inventory;

use app\admin\model\information\Shelves;
use think\Model;
use traits\model\SoftDelete;
use think\Db;
use app\admin\model\Kisapi;


class Temporary extends Model
{

    //use SoftDelete;

    // 表名
    protected $name = 'temporary';
    
    protected $pk = 'id';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'order_list_text',
        'grounding_list_text',
        'status_list_text',
        'pass_status_list_text'
    ];
    
    protected $type = [
      'quantity' => 'quantity',
      'store_quantity' => 'quantity'
    ];

    
    public function getOrderListList()
    {
        return [
            '1' => __('Order_list 1'), 
            '2' => __('Order_list 2'), 
            '3' => __('Order_list 3'),
            '4' => __('Order_list 4'),
            '5' => __('Order_list 5'),
            '6' => __('Order_list 6'),
            '7' => __('Order_list 7'),
            '8' => __('Order_list 8'),
            '9' => __('Order_list 9'),
            '10' => __('Order_list 10'),
            '11' => __('Order_list 11'),
            '12' => __('Order_list 12'),

            '14' => __('Order_list 14'),
            '15' => __('Order_list 15'),
            '16' => __('Order_list 16')
        ]
        ;
    }

    public function getGroundingListList()
    {
        return ['0' => __('Grounding_list 0'), '1' => __('Grounding_list 1')];
    }

    public function getStatusListList()
    {
        return ['0' => __('Status_list 0'), '1' => __('Status_list 1'),'2' => __('Status_list 2')];
    }

    public function getPassStatusListList()
    {
        return ['0' => __('Pass_Status_list 0'), '1' => __('Pass_Status_list 1'),'2' => __('Pass_Status_list 2')];
    }

    public function getShelvesTypeList()
    {
        return ['1' => __('Shelves_type 1'), '2' => __('Shelves_type 2')];
    }

    public function getSellInOutList()
    {
        return ['0' => __('Sell_in_out 0'), '1' => __('Sell_in_out 1'),'2' => __('Sell_in_out 2')];
    }

    public function getOrderListTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['order_list']) ? $data['order_list'] : '');
        $list = $this->getOrderListList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getGroundingListTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['grounding_list']) ? $data['grounding_list'] : '');
        $list = $this->getGroundingListList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusListTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusListList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getPassStatusListTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pass_status']) ? $data['pass_status'] : '');
        $list = $this->getPassStatusListList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    
    public function getShelvesTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['shelves_type']) ? $data['shelves_type'] : '');
        $list = $this->getShelvesTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function warehouse(){
        return $this->hasOne('\app\admin\model\information\Warehouse','id','warehouse_id')->field('id,name,number');
    }
    
    // Ranchang 是关联的模型名  ranchang_id 是外键  id 是主键  LEFT 是join类型
    // setEagerlyType(0) 设置预载入方式  2个值  0  是 JOIN方式   1  是 IN 方式
    public function shelves(){
        return $this->hasOne('\app\common\model\Shelves','id','shelves_id',[],'left')->field('id,name,number')->setEagerlyType(0);
    }
    
    /* public function inventory(){
        return $this->hasOne('\app\common\model\Inventory','number','number',[],'left')->field('id,name');
    }
 */
    
    //判断是否能删本地的临时单据数据，先判断u8，无则删
    //params 
    public static function checkU8ToDeleteFlag($tempID){
        if($tempID){
            $temp = self::get($tempID);
            if(!$temp){
                exception('参数错误,找不到PDA单据');
            }

            $get_temp_id = db('temporary')->where([
                'source_code' => substr($temp->order_code, 2)
            ])
            ->whereOr([
                'source_code' => $temp->order_code,
            ])
            ->where([
                'status' => 1,
                'id' => ['NEQ',$tempID]
            ])
            ->value('id');
            
            if($get_temp_id&&$tempID!=$get_temp_id){
                exception('该PDA单已被其他单据引用，无法删除!');
            }
            
            //账套编码010湖南 001广东味业
            $accnum = Shelves::getErpAccNum($temp->shelves_id,$temp->order_list);

            //这里调接口，判断金蝶有无单
            if($temp->u8_code){
                $kis_order = [];
                $u8_code_arr = explode(',',$temp->u8_code);
                foreach($u8_code_arr as $code){
                    $api_name = 'getIcStockbill';
                    if($temp->order_list==16){
                        $api_name = 'getIcchange';
                    }
                    $kis_order = Kisapi::getDataByName($api_name,[
                        'fBillNo'=>$code,
                        'fAcctNum' => $accnum
                    ],false,true);

                    if($kis_order){
                        exception('单据已被KIS引用,无法删除');
                    }
                }
            }

            if(isset($temp->kis_dbcode)&&$temp->kis_dbcode){
                $kis_order = Kisapi::getDataByName('getIcStockbill',['fBillNo'=>$temp->kis_dbcode],false,true);
                if($kis_order){
                    if($temp->order_list==16){
                        //组件单，这里存放其他入库单
                        exception('单据已被KIS其他入库单引用,无法删除');
                    }else{
                        exception('单据已被KIS材料出库单引用,无法删除');
                    }
                }
            }

            if(isset($temp->kis_work_outcode)&&$temp->kis_work_outcode){
                $kis_order = Kisapi::getDataByName('getIcStockbill',['fBillNo'=>$temp->kis_work_outcode],false,true);
                if($kis_order){
                    if($temp->order_list==16){
                        //组件单，这里存放其他出库单
                        exception('单据已被KIS其他出库单引用,无法删除');
                    }else{
                        if($temp->order_list==14){
                            exception('单据已被KIS产品入库单引用,无法删除');
                        }else{
                            exception('单据已被KIS材料出库单引用,无法删除');
                        }
                    }
                }
            }
        }
    }
}